#include "stdafx.h"
#include "FrameWork.h"
#include "D3DWrap.h"

#include "../DXUT/dxstdafx.h"

HWND				RenderWnd;
TD3DWindow			d3dWin;
CFirstPersonCamera	Camera;


void RW_InitializeCamera()
{
    D3DXVECTOR3   vFromPt(0, 0, 0);
    D3DXVECTOR3   vLookatPt(1.0f, 0, 0);

	Camera.SetViewParams( &vFromPt, &vLookatPt );
	Camera.SetScalers( 0.01f, 15.0f );
	Camera.SetDrag( true );
	Camera.SetEnableYAxisMovement( true );

    D3DXVECTOR3 vMin = D3DXVECTOR3(1.5f,0.0f,1.5f);
    D3DXVECTOR3 vMax = D3DXVECTOR3(13.5f,10.0f,18.5f);
    Camera.SetClipToBoundary( true, &vMin, &vMax ); 

	// TODO[WWL] : move following code into OnResizeWindow
	Camera.SetProjParams( D3DX_PI/4, (float)DEFAULT_RWINDOW_WIDTH / (float)DEFAULT_WINDOW_HEIGHT, 0.2f, 30.0f );
}

void RW_UpdateCamera()
{
	double fTime, fAbsTime; 
	float fElapsedTime;

	DXUTGetGlobalTimer()->GetTimeValues( &fTime, &fAbsTime, &fElapsedTime );
	Camera.FrameMove(fElapsedTime);
}

// Process messages for the render window
LRESULT CALLBACK	RenderWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	Camera.HandleMessages( hWnd, message, wParam, lParam );

	switch (message)
	{
	case WM_DESTROY :
		d3dWin.Destroy();
		PostQuitMessage(0);
		break;

	case WM_PAINT :
		// update camera
		RW_UpdateCamera();

		// render
		d3dWin.Render();
		ValidateRect( RenderWnd, NULL );
		break;

	case WM_CREATE :
		RW_InitializeCamera();
		break;

	default :
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	
	return 0;
}
